package com.company.indicationshots.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.company.indicationshots.model.Score;

import java.util.ArrayList;
import java.util.List;
//射靶分析页面坐标系绘制
public class AnalyseCoordinateSystemView extends View {

    private Paint paint;
    private int bulletCount;

    private List<Score> coordList = new ArrayList<>();

    public AnalyseCoordinateSystemView(Context context) {
        super(context);
        init();
    }

    public AnalyseCoordinateSystemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnalyseCoordinateSystemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setData(int bulletCount, List<Score> coordList) {
        this.bulletCount = bulletCount;
        this.coordList = coordList;
        init();
        invalidate();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int originX = 5;
        int endX = width - 5;
        int originY = height - 30;
        int endY = 30;
        float gridSpacingX = (float) (width - 10) / (6 * 10); // X轴网格间距


        //绘制左侧
        canvas.drawLine(originX, originY, originX, endY, paint);

        paint.setStrokeWidth(1);
        paint.setColor(Color.BLACK);
        //绘制底部
        canvas.drawLine(originX, originY, endX, originY, paint);

        //绘制偏移曲线图顶部x轴
        canvas.drawLine(originX, endY, endX, endY, paint);

        //绘制右侧下半部分
        canvas.drawLine(endX, originY, endX, 220, paint);

        //绘制右侧上半部分
        canvas.drawLine(endX, 160, endX, endY, paint);

        //绘制偏移曲线图的底部
        canvas.drawLine(originX, 160, endX, 160, paint);

        //绘制摆动曲线图顶部
        canvas.drawLine(originX, 220, endX, 220, paint);

        //绘制摆动曲线中间轴
        canvas.drawLine(originX, 363, endX, 363, paint);

        //绘制背景矩形
        paint.setColor(Color.parseColor("#B2B2B2"));
        paint.setStyle(Paint.Style.FILL);
        int singleHeight1 = 26;
        int rectLeft = originX + 3;
        int rectTop1 = endY + singleHeight1;
        int rectRight = endX - 1;
        int rectBottom1 = endY + singleHeight1 * 2;
        canvas.drawRect(rectLeft, rectTop1, rectRight, rectBottom1, paint);

        rectTop1 = rectTop1 + singleHeight1 * 2;
        rectBottom1 = rectBottom1 + singleHeight1 * 2;
        canvas.drawRect(rectLeft, rectTop1, rectRight, rectBottom1, paint);

        float singleHeight2 = 28.6f;
        float rectTop2 = 220 + singleHeight2;
        float rectBottom2 = 220 + singleHeight2 * 2;
        canvas.drawRect(rectLeft, rectTop2, rectRight, rectBottom2, paint);

        rectTop2 = rectTop2 + singleHeight2 * 2;
        rectBottom2 = rectBottom2 + singleHeight2 * 2;
        canvas.drawRect(rectLeft, rectTop2, rectRight, rectBottom2, paint);

        rectTop2 = rectTop2 + singleHeight2 * 3;
        rectBottom2 = rectBottom2 + singleHeight2 * 3;
        canvas.drawRect(rectLeft, rectTop2, rectRight, rectBottom2, paint);

        rectTop2 = rectTop2 + singleHeight2 * 2;
        rectBottom2 = rectBottom2 + singleHeight2 * 2;
        canvas.drawRect(rectLeft, rectTop2, rectRight, rectBottom2, paint);

        paint.setStrokeWidth(1);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        //绘制偏移曲线图X轴刻度
        int j = 0;
        for (float x = originX + gridSpacingX; x < endX; x += gridSpacingX) {
            j++;
            if (j % 10 != 0) {
                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(1);
                canvas.drawLine(x, endY, x, endY + 7, paint);
            } else {
                if (j == 50) {
                    paint.setColor(Color.RED);
                    paint.setStrokeWidth(3);
                    canvas.drawLine(x, endY, x, 160, paint);
                } else {
                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(1);
                    canvas.drawLine(x, endY, x, 160, paint);
                }
            }
        }

        //绘制摆动曲线中间轴刻度
        j = 0;
        for (float x = originX + gridSpacingX; x < endX; x += gridSpacingX) {
            j++;
            if (j % 10 != 0) {
                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(1);
                canvas.drawLine(x, 356, x, 370, paint);
            } else {
                if (j == 50) {
                    paint.setColor(Color.RED);
                    paint.setStrokeWidth(3);
                    canvas.drawLine(x, 220, x, originY, paint);
                } else {
                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(1);
                    canvas.drawLine(x, 220, x, originY, paint);
                }
            }
        }

        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);
        paint.setTextSize(15);

        //绘制偏移曲线图X轴标签
        for (int x = 0; x <= 6; x++) {
            float actualX = originX + (x * gridSpacingX * 10); // 将逻辑坐标转换为实际坐标
            if (actualX <= endX) {
                // 绘制刻度标签
                String label = String.valueOf(x - 5);
                float textWidth = paint.measureText(label);
                canvas.drawText(label, actualX - textWidth / 2, 27, paint);
            }
        }

        //绘制摆动曲线图X轴标签
        for (int x = 0; x <= 6; x++) {
            float actualX = originX + (x * gridSpacingX * 10); // 将逻辑坐标转换为实际坐标
            if (actualX <= endX) {
                // 绘制刻度标签
                String label = String.valueOf(x - 5);
                float textWidth = paint.measureText(label);
                canvas.drawText(label, actualX - textWidth / 2, 217, paint);
            }
        }


        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setTextSize(18);
        paint.setAntiAlias(true);

        //绘制偏移曲线图y轴刻度
        float yScaleX = gridSpacingX * 50 + 6;
        float yScaleY1 = endY + singleHeight1 - 5;
        for (int i = 10; i >= 6; i--) {
            if (i % 2 == 0) {
                paint.setColor(Color.BLACK);
                if (i != 10){
                    yScaleX = gridSpacingX * 50 + 9;
                    yScaleY1 = yScaleY1 + singleHeight1;
                }
            } else {
                paint.setColor(Color.WHITE);
                yScaleX = gridSpacingX * 50 + 9;
                yScaleY1 = yScaleY1 + singleHeight1;
            }
            canvas.drawText(String.valueOf(i), yScaleX, yScaleY1, paint);
        }

        //绘制摆动曲线图y轴刻度
        float yScaleY2 = 220 + singleHeight2 / 2 + 2;
        for (int i = 6; i <= 10; i++) {
            if (i % 2 == 0) {
                paint.setColor(Color.BLACK);
                if (i != 6){
                    if (i == 10){
                        yScaleX = gridSpacingX * 50 + 6;
                    }
                    yScaleY2 = yScaleY2 + singleHeight2;
                }
            } else {
                paint.setColor(Color.WHITE);
                yScaleY2 = yScaleY2 + singleHeight2;
            }
            canvas.drawText(String.valueOf(i), yScaleX, yScaleY2, paint);
        }

        float yScaleY3 = 363 + singleHeight2 - 5;
        for (int i = 10; i >= 6; i--) {
            if (i % 2 == 0) {
                paint.setColor(Color.BLACK);
                if (i != 10){
                    yScaleX = gridSpacingX * 50 + 9;
                    yScaleY3 = yScaleY3 + singleHeight2;
                }
            } else {
                paint.setColor(Color.WHITE);
                yScaleX = gridSpacingX * 50 + 9;
                yScaleY3 = yScaleY3 + singleHeight2;
            }
            canvas.drawText(String.valueOf(i), yScaleX, yScaleY3, paint);
        }

        paint.setColor(Color.RED);
        paint.setTextSize(20);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        canvas.drawText("击发时刻",gridSpacingX * 47, 180, paint);

        canvas.drawText("击发时刻",gridSpacingX * 47,originY + 20,paint);

        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);

        canvas.drawLine(0,182,width,182,paint);
        canvas.drawLine(width,182,width,202,paint);

        paint.setColor(Color.parseColor("#363636"));
        canvas.drawLine(0,182,0,202,paint);
        canvas.drawLine(0,202,width,202,paint);

        paint.setColor(Color.parseColor("#888888"));
        canvas.drawLine(0,188,width - 10,188,paint);
        canvas.drawLine(0,196,width - 10,196,paint);
        canvas.drawLine(width - 10,188,width - 10,196,paint);

        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0,189,width - 10,195,paint);

        paint.setColor(Color.parseColor("#9E009E"));
        canvas.drawRect(0,184,20,200,paint);

        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawLine(0,184,0,200,paint);

        paint.setColor(Color.BLACK);
        canvas.drawLine(20,184,20,200,paint);


//        Paint paint = new Paint();
//        paint.setColor(Color.RED);
//        paint.setTextAlign(Paint.Align.CENTER);
//        paint.setAntiAlias(true);

//        Score pre = null;
//        Score curr = null;
//        float preX = 0;
//        float preY = 0;
//        float currX = 0;
//        float currY = 0;
//        for (int i = 0; i < coordList.size(); i++) {
//            if (i > 0) {
//                pre = coordList.get(i - 1);
//            }
//            curr = coordList.get(i);
//
//            if (i > 0) {
//                preX = originX + pre.getNum() * ((float) gridSpacingX / diff);
//                preY = originY - ((pre.getRing() - 4.0f) * gridSpacingY);
//                if (pre.getRing() < 5.0f) {
//                    preY = originY - ((3.5f - 4.0f) * gridSpacingY);
//                }
//            }
//            currX = originX + curr.getNum() * ((float) gridSpacingX / diff);
//            currY = originY - ((curr.getRing() - 4.0f) * gridSpacingY);
//
//            if (curr.getRing() < 5.0f) {
//                currY = originY - ((3.5f - 4.0f) * gridSpacingY);
//                paint.setTextSize(12);
//                canvas.drawText("•", currX, currY + 3, paint);
//            } else {
//                paint.setTextSize(17);
//                canvas.drawText("+", currX, currY + 6, paint);
//            }
//
//            if (i > 0) {
//                canvas.drawLine(preX, preY, currX, currY, paint);
//            }
//        }

    }

}
